package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.service.AdminAdminService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
public class AdminAdminController{

    @Autowired
    AdminAdminService adminService;

    @GetMapping("/list")
    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "管理员"},button = "查询")
    public Object getAdminList(HttpServletRequest req) throws IOException {
        //第一步：获取req中&连接的参数page&limit&username&sort&order
        Map<String, String[]> parameterMap = req.getParameterMap();
        //第二步：调用service处理业务，并把参数传递下去，获取adminList
        List<MarketAdmin> adminList = adminService.list(parameterMap);
        //第三步：处理成标准响应的格式然后响应回去
        return ResponseUtil.okList(adminList);
    }

    @PostMapping("/create")
    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "管理员"},button = "新增")
    public Object createAdmin(HttpServletRequest req) throws IOException {
        //获取req中的json字符串
        String reqJson = req.getReader().readLine();
        //调用service处理业务
        return adminService.create(reqJson);
    }

    @PostMapping("/update")
    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "管理员"},button = "更新")
    public Object updateAdmin(HttpServletRequest req) throws IOException {
        //获取req中的json字符串
        String reqJson = req.getReader().readLine();
        //调用service处理业务
        return adminService.update(reqJson);
    }

    @PostMapping("delete")
    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "管理员"},button = "删除")
    public Object deleteAdmin(HttpServletRequest req) throws IOException {
        //获取req中的json字符串
        String reqJson = req.getReader().readLine();
        //调用service处理业务
        return adminService.delete(reqJson);
    }
}
